package hoyo.zqy.basic.service.impl;

import hoyo.zqy.basic.exception.BusinessException;
import hoyo.zqy.basic.service.IVerifyCodeService;
import hoyo.zqy.basic.util.SmsUtils;
import hoyo.zqy.basic.util.StrUtils;
import hoyo.zqy.basic.util.VerifyCodeUtils;
import hoyo.zqy.user.domain.User;
import hoyo.zqy.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Override
    public String getImageCody(String key) {
        //通过工具类生产图形验证码 - 字符串
        String imageCode = StrUtils.getComplexRandomString(4);
        //将key和验证码保存在redis(限时5分钟)
        redisTemplate.opsForValue().set(key, imageCode, 5, TimeUnit.MINUTES);
        //将验证码写入图片内，使用base64编码 - 调用工具类实现
        String verifyCode = VerifyCodeUtils.verifyCode(115, 40, imageCode);
        return verifyCode;
    }

    @Override
    public void getsmsCode(Map<String, String> map) {
        //获取参数
        String phone = map.get("phone");
        String imageCode = map.get("imageCode");
        String imageCodeKey = map.get("imageCodeKey");
        //空值校验
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(imageCode)){
            throw new BusinessException("请写入信息");
        }
        //图形验证码
        Object obj = redisTemplate.opsForValue().get(imageCodeKey);
        if (obj == null){
            throw new BusinessException("图形验证码已失效");
        }
        if (!imageCode.equalsIgnoreCase(obj.toString())){
            throw new BusinessException("图形码错误");
        }
        //手机号码是否被注册过
        User user = userMapper.loadByPhone(phone);
        if (user != null){
            throw new BusinessException("已经被人捷足先登了");
        }
        //获取手机验证码
        Object phoneCodeObj = redisTemplate.opsForValue().get("register:" + phone);
        String code = null;
        //判断验证码是否过期
        if (phoneCodeObj==null){//过期了或者第一次
            code = StrUtils.getRandomString(4);
        }else {//没有过期
            //获取上一次的时间
            long oldTime = Long.parseLong(phoneCodeObj.toString().split(":")[1]);
            //获取现在的时间
            long thisTime = System.currentTimeMillis();
            //相减判断是否在一分钟内发送
            if ((thisTime-oldTime)<1*60*1000){//判断成功就说明在一分钟内发送了一次请求
                //告知用户操作违规
                throw new BusinessException("抱歉您的操作违规了,请勿重复发送验证码");
            }
            code = phoneCodeObj.toString().split(":")[0];
        }
        //手机验证码保存到redis
        redisTemplate.opsForValue().set("register:"+phone, code+":"+System.currentTimeMillis(),3,TimeUnit.MINUTES);
        //发送验证码
       // SmsUtils.sendSms(phone,"验证码："+code+",请在3分钟内使用" );
        System.out.println(phone);
        System.out.println("验证码："+code+",请在3分钟内使用");
    }

    /**
     * 邮箱验证码发送
     * @param map
     */
    @Override
    public void binderSmsCode(Map<String, String> map) {
        //获取参数
        String phone = map.get("phone");
        //空值校验
        if (StringUtils.isEmpty(phone) ){
            throw new BusinessException("请写入信息");
        }
        //获取手机验证码
        Object phoneCodeObj = redisTemplate.opsForValue().get("binder:" + phone);
        String code = null;
        //判断验证码是否过期
        if (phoneCodeObj==null){//过期了或者第一次
            code = StrUtils.getRandomString(4);
        }else {//没有过期
            //获取上一次的时间
            long oldTime = Long.parseLong(phoneCodeObj.toString().split(":")[1]);
            //获取现在的时间
            long thisTime = System.currentTimeMillis();
            //相减判断是否在一分钟内发送
            if ((thisTime-oldTime)<1*60*1000){//判断成功就说明在一分钟内发送了一次请求
                //告知用户操作违规
                throw new BusinessException("抱歉您的操作违规了,请勿重复发送验证码");
            }
            code = phoneCodeObj.toString().split(":")[0];
        }
        //手机验证码保存到redis
        redisTemplate.opsForValue().set("binder:"+phone, code+":"+System.currentTimeMillis(),3,TimeUnit.MINUTES);
        //发送验证码
        // SmsUtils.sendSms(phone,"验证码："+code+",请在3分钟内使用" );
        System.out.println(phone);
        System.out.println("验证码："+code+",请在3分钟内使用");
    }
}
